package com.lp.controller;

import com.lp.po.*;
import com.lp.service.*;
import com.lp.userException.CustomException;
import com.lp.utils.Depository;
import com.lp.utils.ImageUtil;
import com.lp.utils.MDUtils;
import com.lp.utils.RegistUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/*
* 用户身份的控制器
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;
    @Resource
    private CollegeService collegeService;
    @Resource
    private UserService userService;
    @Resource
    private CommentService commentService;
    @Resource
    private ArgumentService argumentService;

    RegistUtil registUtil=new RegistUtil();
    Depository depository=new Depository();

    // 删除景点评价
    @RequestMapping(value = "/removeTalk",method = RequestMethod.GET)
    public String removeTalk(Integer id) throws Exception {
        commentService.delectcomment(id);
        return "redirect:/student/showTalk";
    }

    //景点评价
    @RequestMapping(value = "/showTalk")
    public String showTalk(Model model,Integer page) throws Exception {
        List<Comment> list=null;
        int talkNumber=0;
        //获得用户id
        int studentid=studentService.findIdByUsername(Depository.getUSERNAME());
        //页码对象
        PagingVO pagingVO = new PagingVO();
        //设置总页数
        pagingVO.setTotalCount(this.commentService.getCommentCount());
        if (page == null || page == 0) {
            pagingVO.setToPageNo(1);
            list = commentService.findCommentWithStudent(studentid,1);
            talkNumber=commentService.getCountWithStudent(studentid);
        } else {
            pagingVO.setToPageNo(page);
            list = commentService.findCommentWithStudent(studentid,page);
            talkNumber=commentService.getCountWithStudent(studentid);
        }
        model.addAttribute("selectedTalkList", list);
        model.addAttribute("pagingVO", pagingVO);
        model.addAttribute("num", talkNumber);
        return "student/showTalk";
    }

    //进入首页
    @RequestMapping(value = "/showStudent")
    public String stuCourseShow(Model model, Integer page) throws Exception {
        Student stu=studentService.findself(Depository.getUSERNAME());
        model.addAttribute("student",stu);
        return "student/showStudent";
    }

    //打开修改页面
    @RequestMapping("/editStudent")
    public String editStudent(Model model) throws Exception {
        Student stu=studentService.findself(Depository.getUSERNAME());
        List<College> list = collegeService.findAll();
        model.addAttribute("student",stu);
        model.addAttribute("collegeList", list);
        return "student/editStudent";
    }

    //修改个人信息
    @RequestMapping("/doEditStudent")
    public String doEditStudent(Model model,Student student, MultipartFile file) throws Exception {
        if(!file.isEmpty()) {
            String uploadUrl = argumentService.getByName("uploadUrl").getValue();
            String newFileName= ImageUtil.getNewFileName(file,uploadUrl);
            student.setStudentimg(newFileName);
        }
        studentService.updataById(student.getUserid(), student);
        //  获取到在user表中的id  依据id进行更新
        int userid=userService.findUseridByStudentName(Depository.getUSERNAME());
        User user=new User();
        //user.setPassword(MDUtils.getMD5Password(student.getPassword(),registUtil.getSalt(student.getUsername())).toString());
        user.setUsername(student.getUsername());
        userService.updateByUserId(user.getUsername(),userid);
        depository.setUSERNAME(student.getUsername());
        model.addAttribute("msg","本账户已修改，请重新登陆");
        return "login";
    }

    //修改密码
    @RequestMapping(value = "/passwordRest")
    public String passwordRest() throws Exception {
        return "student/passwordRest";
    }

    //修改密码
    @RequestMapping(value = "/doPasswordRest")
    public String doPasswordRest(String password1, String password2,Model model) throws Exception {
        if (password1.equals(password2)) {
            if((password1.indexOf(" ")!=-1) || (password2.indexOf(" ")!=-1)){
                throw new CustomException("密码格式有错误，请认真填写");
            }
            User user = new User();
            user.setPassword(MDUtils.getMD5Password(password1,registUtil.getSalt(Depository.getUSERNAME())).toString());
            userService.updatePasswordWithStudent(user.getPassword(),Depository.getUSERNAME());
            model.addAttribute("msg","本账户密码已重置，请重新登陆");
        }
        return "login";
    }

    // 院系老师(待定)
    @RequestMapping(value = "/showTeacherWithSelf")
    public String overCourse(Model model) throws Exception {
        return "student/showTeacher";
    }

}
